package com.example.interview;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.json.JSONException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.LinkedList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

public class FirstFragment extends Fragment {
    public interface onItemClickedListener {
        void onItemClicked(Item item);
    }

    final RecyclerView.OnScrollListener onScrollListener;
    final onItemClickedListener onItemClickedListener;

    public FirstFragment(RecyclerView.OnScrollListener onScrollListener, FirstFragment.onItemClickedListener onItemClickedListener) {
        this.onScrollListener = onScrollListener;
        this.onItemClickedListener = onItemClickedListener;
    }

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        doRequest(view);
    }

    public void doRequest(View view) {
        final List<Item> itemList = new LinkedList<>();
        new Thread(() -> {
            try {
                Utils.getTeachers(itemList);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }).start();
        synchronized (itemList) {
            try {
                itemList.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(new RecyclerView.Adapter() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new RecyclerView.ViewHolder(new TextView(parent.getContext())) {
                };
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
                ((TextView) holder.itemView).setText(itemList.get(position).name);
                ((TextView) holder.itemView).setPadding(100, 100, 100, 100);
                holder.itemView.setOnClickListener(view1 -> onItemClickedListener.onItemClicked(itemList.get(position)));
            }

            @Override
            public int getItemCount() {
                return itemList.size();
            }
        });

        recyclerView.addOnScrollListener(onScrollListener);
    }
}